package vip.xiaonuo.sys.modular.monitoringgather.config;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Component
@Slf4j
public class ExcelHelper {

    /**
     * excel文件导出到本地
     * @param fileName
     * @param entityList
     * @return
     */
    public static <T> void exportFileNayive(String fileName, List<T> entityList) {
        File file = new File(fileName);
        ExcelWriter bigWriter = ExcelUtil.getBigWriter();
        try {
            file.createNewFile();
            bigWriter.setDestFile(file);
            //将数据写入到ExcelWriter的workbook中
            bigWriter.write(entityList);
            //将数据写入excel表
            bigWriter.flush();
        } catch (IOException e) {
            throw new RuntimeException("SYS_EXPORT_ERROR");
        } finally {
            //关闭资源
            bigWriter.close();
        }
    }

    /**
     * excel文件导出到浏览器
     * @param entityList
     * @return
     */
    public static <T> void exportFile(HttpServletResponse httpServletResponse, String fileName, List<T> entityList) {
        ExcelWriter bigWriter = ExcelUtil.getBigWriter();
        try {
            ServletOutputStream outputStream = httpServletResponse.getOutputStream();
            //将数据写入到ExcelWriter的workbook中
            bigWriter.write(entityList);
            //将数据写入excel表
            httpServletResponse.reset();
            httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            httpServletResponse.setContentType("application/octet-stream; charset=UTF-8");
            bigWriter.flush(outputStream);
        } catch (IOException e) {
            throw new RuntimeException("SYS_EXPORT_ERROR");
        } finally {
            bigWriter.close();
        }
    }

    /**
     * 本地文件读取
     * @param file
     * @param beanClass
     * @return
     */
    public static <T> List<T> importNaticeFile(File file, Class<T> beanClass) {
        ExcelReader excelReader = ExcelUtil.getReader(file);
        return excelReader.readAll(beanClass);
    }

    /**
     * excel文件的导入
     * @param file
     * @return
     */
    public static <T> List<T> importFile(MultipartFile file, Class<T> beanClass) {
        if (file.isEmpty()) {
            throw new RuntimeException();
        }
        //1 获取上传文件输入流
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
            //2 io流给ExcelReader
            ExcelReader excelReader = ExcelUtil.getReader(inputStream);
            return excelReader.readAll(beanClass);
        } catch (IOException e) {
            log.error("importFile error, msg:{}", e.getMessage(), JSONUtil.toJsonPrettyStr(e.getStackTrace()));
            throw new RuntimeException("SYS_IMPORT_ERROR");
        }
    }

}